//
//  MessageFromCell.m
//  HotWord
//
//  Created by Rainbow on 8/11/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "MessageFromCell.h"

#define MESSAGE_FROM_CONTENT_LABEL_WIDTH   175
#define MESSAGE_FROM_CONTENT_LABEL_MIN_HEIGHT   21
#define MESSAGE_FROM_CELL_MIN_HEIGHT   45

@implementation MessageFromCell
@synthesize contentLabel;
@synthesize talkBgView;
@synthesize headImageView;
@synthesize timeView;
@synthesize timeLabel;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setMessage:(Message *)message
{
    CGSize labelSize = [MessageFromCell getContentHeightByContent:message.content];
    self.contentLabel.height = labelSize.height;
    self.contentLabel.width = labelSize.width;
    self.talkBgView.width = labelSize.width + 50;
    self.talkBgView.height =self.contentLabel.height + 24;
    self.timeView.layer.cornerRadius = 6;
    self.timeLabel.text = [UtilHelper getDateString:[message.timestamp longLongValue] withFormat:@"yyyy-MM-dd hh:mm"];
    self.contentLabel.text = message.content;
    [self.headImageView loadImage:message.dialogAvator];

}


+ (CGSize)getContentHeightByContent:(NSString *)content
{
    CGSize labelSize = [content sizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:CGSizeMake(MESSAGE_FROM_CONTENT_LABEL_WIDTH, 1000) lineBreakMode:UILineBreakModeWordWrap];
    if (labelSize.width < 45) {
        labelSize.width = 45;
    }
    if (labelSize.height < MESSAGE_FROM_CONTENT_LABEL_MIN_HEIGHT) {
        return CGSizeMake(labelSize.width, MESSAGE_FROM_CONTENT_LABEL_MIN_HEIGHT) ;
    }
    else {
        return labelSize;
    }
}

+ (CGFloat)getCellHeightByContent:(NSString *)content
{
    return MESSAGE_FROM_CELL_MIN_HEIGHT + [MessageFromCell getCellHeightByContent:content] - MESSAGE_FROM_CONTENT_LABEL_MIN_HEIGHT;
}

- (void)dealloc {
    [contentLabel release];
    [headImageView release];
    [talkBgView release];
    [timeView release];
    [timeLabel release];
    [super dealloc];
}
@end
